package com.biallen.datastructure.queue;

import java.util.Scanner;

public class MyArrayQueueDemo {
    public static void main(String[] args) {

        MyQueue myQueue = new MyQueue(3);
        char key =' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop){
            System.out.println("p(display): 显示队列");
            System.out.println("a(add): 增加队列");
            System.out.println("e(exit): 退出队列");
            System.out.println("h(head): 获取头队列");
            System.out.println("g(getQueue): 获取队列");
            key=scanner.next().charAt(0);
            switch (key){
                case 'p':
                    myQueue.display();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int value = scanner.nextInt();
                    try {
                        myQueue.addQueue(value);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        int queue = myQueue.getQueue();
                        System.out.println("取出的数据是:"+queue);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int head = myQueue.head();
                        System.out.println("取出的头部数据是:"+head);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop=false;
                    break;
            }
        }
    }
}

class MyQueue {

    private int front;//队列头

    private int tail;//队列尾

    private int maxSize;//队列长度

    private int[] data;

    public MyQueue(int maxSize) {
        this.data = new int[maxSize];
        this.maxSize = maxSize;
        front = -1;
        tail = -1;
    }

    public  boolean isFull(){
        return  tail==maxSize-1;
    }

    public  boolean isEmty(){
        return  front == tail;
    }

    public  void  addQueue(int da){
        if(isFull()){
            System.out.println("队列已满");
          return;
        }
        tail++;
        data[tail]=da;
    }

    public  int  getQueue(){
        if(isEmty()){
            throw  new RuntimeException("队列为空,无法取数据---");
        }
        front++;
        return  data[front];
    }

    /**
     * 获取头节点数据
     * @return
     */
    public  int head(){
        if(isEmty()){
            throw  new RuntimeException("队列为空,无法取数据---");
        }
        return  data[front+1];
    }

    public void display(){
        for (int i = 0; i < data.length; i++) {
            System.out.printf("arr[%d]=%d\n",i,data[i]);
        }


    }
}